package org.itboys.core.controller.web.member;

import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.common.UserTypeEnum;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/11/26
 * @Description：用户
 */
@Controller
@RequestMapping("sunday/web/member")
public class SundayMemberController extends BaseController {

    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SystemUserService userService;

    @RequestMapping("/lease/index")
    public String LIndex(HttpServletRequest request, HttpServletResponse response) {
        return "/sunday/member/lease/index";
    }

    @RequestMapping("/supplier/index")
    public String SIndex(HttpServletRequest request, HttpServletResponse response) {
        return "/sunday/member/supplier/index";
    }

    @RequestMapping("/select")
    public void select(HttpServletResponse response, HttpServletRequest request) {
        PageResult<SundayMember> result = memberService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    @RequestMapping("/update")
    public void update(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "id") Long memberId
            , @RequestParam(value = "status") Integer status
    ) {
        SundayMember member = new SundayMember();
        member.setStatus(status);
        if (SundayCommonConstants.USER_STATUS_PASS.equals(status)) {
            member.setUserType(UserTypeEnum.COMPANY.getCode());
        }
        memberService.updateExceptEmpty(memberId, member);
        ResponseMessage.success();
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "id") Long id
            , @RequestParam(value = "realName") String realName
            , @RequestParam(value = "mobile") String mobile
            , @RequestParam(value = "password", required = false) String password
            , @RequestParam(value = "provinceId") Long provinceId
            , @RequestParam(value = "provinceName") String provinceName
            , @RequestParam(value = "cityId") Long cityId
            , @RequestParam(value = "cityName") String cityName
            , @RequestParam(value = "districtId") Long districtId
            , @RequestParam(value = "districtName") String districtName
    ) {
        SundayMember member=new SundayMember(realName,  mobile, UserTypeEnum.SUPPLIER.getCode(), SundayCommonConstants.USER_STATUS_PASS, password,  provinceId,  provinceName,  cityId,  cityName,  districtId,  districtName);
        member.setId(id);
        memberService.save(member);
        ResponseMessage.success();
    }

    @RequestMapping("/choose")
    public String choose(HttpServletRequest request,
                         @RequestParam(value = "isSingle", required = true) Integer isSingle,//是否单选
                         @RequestParam(value = "callBack", required = false) String callBack,//回调参数
                         @RequestParam(value = "param", required = false) String param,
                         Model model) {
        model.addAttribute("param", param);
        model.addAttribute("callBack", callBack);
        if (isSingle == null) {
            return "/sunday/member/choose/choose_erro";
        } else if (isSingle == 1) {
            return "/sunday/member/supplier/choose/choose_single";
        } else if (isSingle == 0) {
            return "/sunday/member/choose/choose_muilt";
        }
        return "/finance/member/choose/choose_erro";
    }

    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,HttpServletResponse response
    ,@RequestParam(value = "id")Long id){
        SundayMember member = memberService.getById(id);
        ResponseMessage.success(member);
    }
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,HttpServletResponse response
            ,@RequestParam(value = "id")Long id){
        memberService.delete(id);
        ResponseMessage.success();
    }
}
